package view;

import interfaces.ViewPanel;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import basic.Date;

import storage.ManageData;
import utils.CommandGenerator;
import utils.DataFormat;

import control.ExpenseManager;
import control.IncomeManager;
import control.LoanManager;
import control.MemberManager;
import control.Shell;
import control.ValidateInput;
import control.ViewPanelManager;

import domain.Budget;
import domain.FamilyMember;
import enums.LoanState;
import javax.swing.JSeparator;

public class InvestmentPlanPanel extends BackgroundPanel implements ViewPanel {
	
	private static String DEFAULT_PARTYINVOLVED = "Bank";
	
	private JLabel lblCurrentIncomeSurplus;
	private JTextField textField_targetPurchase;
	private JLabel lblcurrSaved;
	
	private JLabel lblPromptCustomAmount;
	private JComboBox comboBox_AmountToInvest;
	private JTextField textField_CustomAmount;
	
	private JLabel lblPromptIR;
	private JComboBox comboBox_IR;
	private JTextField textField_CustomIR;
	private JTextField textField_CustomName;
	private JLabel lblPromptCustomIR;
	private JLabel lblPromptCustomName;
	
	private JLabel lblNumberTimeChecks;
	private JLabel lblPromptYearsToInvest;
	private JComboBox comboBox_NumberTimeChecks;
	private JTextField textField_YearsToInvest;
	private JLabel lblPromptFirstTimePeriod;
	private JTextField textField_FirstTimePeriod;
	private JLabel lblPromptSecondTimePeriod;
	private JTextField textField_SecondTimePeriod;
	private JLabel lblPromptThirdTimePeriod;
	private JTextField textField_ThirdTimePeriod;
	private JButton btnCalculateSavings;
	
	private JLabel lblPromptAddInvestment;
	private JComboBox comboBox_InvestmentDecision;
	private JButton btnAddSavings;
	
	private ExpenseManager _expenseMan;
	private IncomeManager _incomeMan;
	private MemberManager _memMan;
	private LoanManager _loanMan;
	private Shell _shell;
	private Double _currentIncomeSurplus;
	private Double _currentSaved;
	private ViewPanelManager _tabPanelMgr;
	private LoanManagerPanel _panel_loanMgr;
	private JSeparator separator;
	private Vector<Double> _ir_list;
	private Vector<String> _irName_list;
	
	//@author A0103947A
	/**
	 * @param expenseMan
	 * @param shell
	 * @param incomeMan
	 * @param memMan
	 * @param loanMan
	 * @param panel_loanMgr
	 * @param tabPanelMgr
	 */
	public InvestmentPlanPanel(ExpenseManager expenseMan, final Shell shell, IncomeManager incomeMan, MemberManager memMan, LoanManager loanMan, LoanManagerPanel panel_loanMgr, ViewPanelManager tabPanelMgr){
		_expenseMan = expenseMan;
		_incomeMan = incomeMan;
		_memMan = memMan;
		_loanMan = loanMan;
		_shell = shell;
		_tabPanelMgr = tabPanelMgr;
		_panel_loanMgr = panel_loanMgr;
		
		_currentIncomeSurplus = new Double(_incomeMan.getTotalAmountOfAll() - _expenseMan.getTotalAmountOfAll() 
				- _loanMan.get_valueInvested() - _loanMan.get_amountFromLoansLent());
		
		btnCalculateSavings = new JButton("Calculate");
		btnCalculateSavings.setVisible(false);
		btnCalculateSavings.setBounds(723, 414, 102, 29);
		btnCalculateSavings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Check if all inputs or valid; if not, return and allow user to make changes
				if(!validateSavingsAnalysis())
					return;
				

				double firstYear, secondYear, thirdYear;
				
				double amountInvested = Double.parseDouble(textField_CustomAmount.getText());
				double interestRate = Double.parseDouble(textField_CustomIR.getText());
				int numberOfInvestments = Integer.parseInt(comboBox_NumberTimeChecks.getSelectedItem().toString());
				
				// If only one investment to check, get value for firstYear from textfield
				// And set secondYear and thirdYear to 0
				if (numberOfInvestments == 1) {
					firstYear = Double.parseDouble(textField_FirstTimePeriod.getText());
					secondYear = 0;
					thirdYear = 0;
				}
				// Else if two investments to check, get value for firstYear and secondYear
				// From respective textfields and set thirdYear to 0
				else if (numberOfInvestments == 2) {
					firstYear = Double.parseDouble(textField_FirstTimePeriod.getText());
					secondYear = Double.parseDouble(textField_SecondTimePeriod.getText());
					thirdYear = 0;
				}
				// Else, there must be three investments to check
				// Get value for firstYear, secondYear, and thirdYear from respective textfeilds
				else {
				firstYear = Double.parseDouble(textField_FirstTimePeriod.getText());
				secondYear = Double.parseDouble(textField_SecondTimePeriod.getText());
				thirdYear = Double.parseDouble(textField_ThirdTimePeriod.getText());
				}
				
				// Call SavingsAnalysis
				SavingsAnalysis mySavingsAnalysis = new SavingsAnalysis(amountInvested, interestRate, numberOfInvestments, firstYear, secondYear, thirdYear);
				
				lblPromptAddInvestment.setVisible(true);
				comboBox_InvestmentDecision.setVisible(true);
			}
		});
		setLayout(null);
		
		JLabel lblPaymentAnalysis = new JLabel("Payment Analysis");
		lblPaymentAnalysis.setBounds(26, 0, 213, 28);
		lblPaymentAnalysis.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		this.add(lblPaymentAnalysis);
		
		JLabel lblCurrentBudgetTitle = new JLabel("Current Balance:");
		lblCurrentBudgetTitle.setBounds(211, 25, 208, 16);
		lblCurrentBudgetTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		this.add(lblCurrentBudgetTitle);
		
		String budgetAmount = null;
		if (_currentIncomeSurplus >= 0) {
			budgetAmount = new String("$" + DataFormat.returnTwoDecimals(_currentIncomeSurplus));
		}
		else {
			budgetAmount = new String("-$" + DataFormat.returnTwoDecimals(Math.abs(_currentIncomeSurplus)));
		}
		lblCurrentIncomeSurplus = new JLabel(budgetAmount);
		lblCurrentIncomeSurplus.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCurrentIncomeSurplus.setBounds(429, 25, 289, 16);
		this.add(lblCurrentIncomeSurplus);
		
		JButton btnSubmitPaymentAnalysis = new JButton("Submit");
		btnSubmitPaymentAnalysis.setBounds(723, 46, 102, 29);
		btnSubmitPaymentAnalysis.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent btnClicked) {
				// Check if all inputs or valid; if not, return and allow user to make changes
				if (!validatePaymentAnalysis())
					return;

				// Otherwise execute Payment Analysis and clear textfield
					double purchaseAmount = Double.parseDouble(textField_targetPurchase.getText());
					PaymentAnalysis myPaymentAnalysis = new PaymentAnalysis(_currentIncomeSurplus, purchaseAmount);
					textField_targetPurchase.setText("");
			}
		});
		
		JLabel lblTargetPurchaseAmount = new JLabel("Target Purchase Amount ($):");
		lblTargetPurchaseAmount.setHorizontalAlignment(SwingConstants.TRAILING);
		lblTargetPurchaseAmount.setBounds(195, 52, 224, 16);
		this.add(lblTargetPurchaseAmount);
		
		textField_targetPurchase = new JTextField();
		textField_targetPurchase.setBounds(429, 46, 289, 28);
		textField_targetPurchase.setHorizontalAlignment(SwingConstants.TRAILING);
		this.add(textField_targetPurchase);
		textField_targetPurchase.setColumns(10);
		this.add(btnSubmitPaymentAnalysis);
		
		JLabel lblSavingsAnalysis = new JLabel("Savings Analysis");
		lblSavingsAnalysis.setBounds(26, 95, 213, 29);
		lblSavingsAnalysis.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		this.add(lblSavingsAnalysis);
		
		JLabel lblAmountSaved = new JLabel("Amount Invested:");
		lblAmountSaved.setHorizontalAlignment(SwingConstants.TRAILING);
		lblAmountSaved.setBounds(273, 132, 146, 16);
		this.add(lblAmountSaved);
		
		lblcurrSaved = new JLabel("$" + DataFormat.returnTwoDecimals(_loanMan.get_valueInvested()));
		lblcurrSaved.setHorizontalAlignment(SwingConstants.RIGHT);
		lblcurrSaved.setBounds(429, 132, 289, 16);
		this.add(lblcurrSaved);
		
		lblPromptFirstTimePeriod = new JLabel("Enter first time period (in whole years)");
		lblPromptFirstTimePeriod.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptFirstTimePeriod.setBounds(6, 354, 413, 16);
		lblPromptFirstTimePeriod.setVisible(false);
		
		comboBox_IR = new JComboBox();
		comboBox_IR.setBounds(429, 218, 289, 27);
		comboBox_IR.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Do nothing if comboBox choice is "(Choose interest rate)"
				
				// If one of the other options are selected, display the appropriate textfields for
				// The next stage of input
				if (comboBox_IR.getSelectedItem().toString() != "(Choose interest rate)") {
				lblPromptCustomIR.setVisible(true);
				textField_CustomIR.setVisible(true); 
				lblPromptCustomName.setVisible(true);
				textField_CustomName.setVisible(true); 
				lblNumberTimeChecks.setVisible(true);
				comboBox_NumberTimeChecks.setVisible(true);
			     // If user inputs "3", "4", or "5", set CustomIR textfield to the appropriate amount
			    if (comboBox_IR.getSelectedItem().toString() != "Other") {
			    	String customName = _irName_list.get(comboBox_IR.getSelectedIndex());
			    	Double customIR = _ir_list.get(comboBox_IR.getSelectedIndex());
			    	String customIRString = DataFormat.returnTwoDecimals(customIR);
			    	textField_CustomIR.setText(customIRString);
			    	textField_CustomName.setText(customName);
			    }
			    // If user inputs "Other", set CustomIR textfield to "" and allow user to add
			    // His/her own input
			    else {
			    	textField_CustomIR.setText("");
			    	textField_CustomName.setText("");
			    }
			}
		}
		});
		
		// Set CustomAmount textfield's visibility to false initially until user
		// Chooses an option on the AmountToInvest combobox
		textField_CustomAmount = new JTextField();
		textField_CustomAmount.setBounds(429, 185, 289, 28);
		textField_CustomAmount.setVisible(false);
		
		comboBox_AmountToInvest = new JComboBox();
		comboBox_AmountToInvest.setBounds(429, 153, 289, 27);
		comboBox_AmountToInvest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent comboClicked) {
				// Do nothing if comboBox choice is "(Choose amount)"
				
			    // If comboBox choice is "All of income surplus", input
			    // value of currSaved into CustomAmount textfield
				if (comboBox_AmountToInvest.getSelectedItem().toString() == "All of income surplus") {		        
			        // Remove commas from currentIncomeSurplus value and use it in textfield
					String valueWithDecimals = new String(DataFormat.returnTwoDecimals(_currentIncomeSurplus));
					String valueWithoutDecimals = new String(valueWithDecimals.replaceAll(",",""));
			   
			        textField_CustomAmount.setText(valueWithoutDecimals);
			    	lblPromptIR.setVisible(true);
					comboBox_IR.setVisible(true);
					lblPromptCustomAmount.setVisible(true);
				    textField_CustomAmount.setVisible(true);
			        
			    }
			    // If comboBox choice is "Other amount", clear CustomAmount textfield
			    // To allow user to input custom amount
			    else if (comboBox_AmountToInvest.getSelectedItem().toString() == "Other amount"){
			    	textField_CustomAmount.setText("");
			    	lblPromptIR.setVisible(true);
					comboBox_IR.setVisible(true);
			    	lblPromptCustomAmount.setVisible(true);
				    textField_CustomAmount.setVisible(true);
			    }
			}
		});
		
		JLabel lblHowMuchDo = new JLabel("How much do you want to invest?");
		lblHowMuchDo.setHorizontalAlignment(SwingConstants.TRAILING);
		lblHowMuchDo.setBounds(169, 158, 250, 16);
		this.add(lblHowMuchDo);
		comboBox_AmountToInvest.setModel(new DefaultComboBoxModel(new String[] {"(Choose amount)", "All of income surplus", "Other amount"}));
		this.add(comboBox_AmountToInvest);
		
		// Set PromptCustomAmount's visibility to false initially until user
		// Chooses an option on the AmountToInvest combobox
		lblPromptCustomAmount = new JLabel("Enter amount to invest");
		lblPromptCustomAmount.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptCustomAmount.setBounds(222, 191, 197, 16);
		lblPromptCustomAmount.setVisible(false);
		this.add(lblPromptCustomAmount);
		textField_CustomAmount.setHorizontalAlignment(SwingConstants.TRAILING);
		this.add(textField_CustomAmount);
		textField_CustomAmount.setColumns(10);
		
		lblPromptIR = new JLabel("What annual interest rate (%) would you like to invest at?");
		lblPromptIR.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptIR.setBounds(41, 223, 378, 16);
		this.add(lblPromptIR);
		lblPromptIR.setVisible(false);

		_irName_list = new Vector<String>();
		_ir_list = new Vector<Double>();
		ManageData.loanInvestmentData(_irName_list, _ir_list);
//		_ir_list.add(0.0);
//		_ir_list.add(0.05);
//		_ir_list.add(3.0);
//		_ir_list.add(8.0);
//
//		_irName_list.add("");
//		_irName_list.add("DBS SAVING PLUS");
//		_irName_list.add("US SAVINGS SECURITIES");
//		_irName_list.add("US DOMESTIC SERIES");
//		comboBox_IR.setModel(new DefaultComboBoxModel(new String[] {"", "DBS SAVING PLUS 0.05%","US SAVINGS SECURITIES 3%","US DOMESTIC SERIES 8%", "Other"}));
		comboBox_IR.addItem("(Choose interest rate)");
		for(int i=1;i<_ir_list.size();i++)
		{
			comboBox_IR.addItem(_irName_list.get(i)+" "+_ir_list.get(i)+"%");
		}
		comboBox_IR.addItem("Other");
		
		this.add(comboBox_IR);
		comboBox_IR.setVisible(false);

		
		lblPromptCustomName = new JLabel("Enter the investment name");
		lblPromptCustomName.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptCustomName.setBounds(232, 255, 187, 16);
		lblPromptCustomName.setVisible(false);
		this.add(lblPromptCustomName);
		
		// Set CustomIR textfield's visibility to false initially until user
		// Chooses an option on the CustomIR combobox
		textField_CustomName = new JTextField();
		textField_CustomName.setBounds(429, 249, 289, 28);
		textField_CustomName.setHorizontalAlignment(SwingConstants.TRAILING);
		textField_CustomName.setVisible(false);
		this.add(textField_CustomName);
		textField_CustomName.setColumns(10);
		// Set PromptCustomIR's visibility to false initially until user
		// Chooses an option on the CustomIR combobox
		lblPromptCustomIR = new JLabel("Enter annual interest rate (%)");
		lblPromptCustomIR.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptCustomIR.setBounds(232, 289, 187, 16);
		lblPromptCustomIR.setVisible(false);
		this.add(lblPromptCustomIR);
		
		// Set CustomIR textfield's visibility to false initially until user
		// Chooses an option on the CustomIR combobox
		textField_CustomIR = new JTextField();
		textField_CustomIR.setBounds(429, 283, 289, 28);
		textField_CustomIR.setHorizontalAlignment(SwingConstants.TRAILING);
		textField_CustomIR.setVisible(false);
		this.add(textField_CustomIR);
		textField_CustomIR.setColumns(10);
		
		lblNumberTimeChecks = new JLabel("Enter number of time periods to check investment after");
		lblNumberTimeChecks.setHorizontalAlignment(SwingConstants.TRAILING);
		lblNumberTimeChecks.setBounds(60, 321, 359, 16);
		this.add(lblNumberTimeChecks);
		lblNumberTimeChecks.setVisible(false);
		
		comboBox_NumberTimeChecks = new JComboBox();
		comboBox_NumberTimeChecks.setVisible(false);
		comboBox_NumberTimeChecks.setBounds(429, 316, 289, 27);
		// Ensure that the appropriate number of textfields pop up based on user's choice
		// Of number of time periods to check investment afte
		comboBox_NumberTimeChecks.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// If option is "Choose number", ensure that all time period-related data fields
				// Are not visible and that all textfields are cleared to ""
				if (comboBox_NumberTimeChecks.getSelectedItem().toString() == "(Choose number)") {
					lblPromptFirstTimePeriod.setVisible(false);
					textField_FirstTimePeriod.setVisible(false);
					lblPromptSecondTimePeriod.setVisible(false);
					textField_SecondTimePeriod.setVisible(false);
					lblPromptThirdTimePeriod.setVisible(false);
					textField_ThirdTimePeriod.setVisible(false);
					textField_FirstTimePeriod.setText("");
					textField_SecondTimePeriod.setText("");
					textField_ThirdTimePeriod.setText("");
				}
				// If option is "1", ensure that first time period-related data fields
				// Are set to visible and others are set to not visible
				if (comboBox_NumberTimeChecks.getSelectedItem().toString() == "1") {
					lblPromptFirstTimePeriod.setVisible(true);
					textField_FirstTimePeriod.setVisible(true);
					lblPromptSecondTimePeriod.setVisible(false);
					textField_SecondTimePeriod.setVisible(false);
					lblPromptThirdTimePeriod.setVisible(false);
					textField_ThirdTimePeriod.setVisible(false);
					btnCalculateSavings.setVisible(true);
				}
				// If option is "2", ensure that first and second-time period related
				// Data fields are set to visible and others is set to not visible
				else if (comboBox_NumberTimeChecks.getSelectedItem().toString() == "2") {
					lblPromptFirstTimePeriod.setVisible(true);
					textField_FirstTimePeriod.setVisible(true);
					lblPromptSecondTimePeriod.setVisible(true);
					textField_SecondTimePeriod.setVisible(true);
					lblPromptThirdTimePeriod.setVisible(false);
					textField_ThirdTimePeriod.setVisible(false);
					btnCalculateSavings.setVisible(true);
				}
				// If option is "3", ensure that all time-period related data fields
				// Are set to visible
				else if (comboBox_NumberTimeChecks.getSelectedItem().toString() == "3") {
					lblPromptFirstTimePeriod.setVisible(true);
					textField_FirstTimePeriod.setVisible(true);
					lblPromptSecondTimePeriod.setVisible(true);
					textField_SecondTimePeriod.setVisible(true);
					lblPromptThirdTimePeriod.setVisible(true);
					textField_ThirdTimePeriod.setVisible(true);
					btnCalculateSavings.setVisible(true);
				}
			}
		});
		comboBox_NumberTimeChecks.setModel(new DefaultComboBoxModel(new String[] {"(Choose number)", "1", "2", "3"}));
		this.add(comboBox_NumberTimeChecks);
		this.add(lblPromptFirstTimePeriod);
		
		textField_ThirdTimePeriod = new JTextField();
		textField_ThirdTimePeriod.setBounds(429, 414, 289, 28);
		textField_ThirdTimePeriod.setVisible(false);
		
		lblPromptSecondTimePeriod = new JLabel("Enter second time period (in whole years)");
		lblPromptSecondTimePeriod.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptSecondTimePeriod.setBounds(6, 387, 413, 16);
		lblPromptSecondTimePeriod.setVisible(false);
		
		textField_FirstTimePeriod = new JTextField();
		textField_FirstTimePeriod.setBounds(429, 348, 289, 28);
		textField_FirstTimePeriod.setVisible(false);
		textField_FirstTimePeriod.setHorizontalAlignment(SwingConstants.TRAILING);
		this.add(textField_FirstTimePeriod);
		textField_FirstTimePeriod.setColumns(10);
		this.add(lblPromptSecondTimePeriod);
		
		textField_SecondTimePeriod = new JTextField();
		textField_SecondTimePeriod.setBounds(429, 381, 289, 28);
		textField_SecondTimePeriod.setVisible(false);
		textField_SecondTimePeriod.setHorizontalAlignment(SwingConstants.TRAILING);
		this.add(textField_SecondTimePeriod);
		textField_SecondTimePeriod.setColumns(10);
		
		lblPromptThirdTimePeriod = new JLabel("Enter third time period (in whole years)");
		lblPromptThirdTimePeriod.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptThirdTimePeriod.setBounds(26, 420, 393, 16);
		lblPromptThirdTimePeriod.setVisible(false);
		this.add(lblPromptThirdTimePeriod);
		textField_ThirdTimePeriod.setHorizontalAlignment(SwingConstants.TRAILING);
		this.add(textField_ThirdTimePeriod);
		textField_ThirdTimePeriod.setColumns(10);
		this.add(btnCalculateSavings);
		
		btnAddSavings = new JButton("Add");
		btnAddSavings.setBounds(723, 512, 102, 29);
		btnAddSavings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent btnClicked) {	
				// Check if all inputs or valid; if not, return and allow user to make changes
				if(!validateInvestmentAdd())
					return;
				
				Date currDate = Date.getCurrentDate();
				int dueYear = currDate.get_year() + Integer.parseInt(textField_YearsToInvest.getText());
				Date dueDate = new Date(currDate.get_day(), currDate.get_month(), dueYear);
				
				String cmd = CommandGenerator.getAddLoanCmd(textField_CustomName.getText(), 
						Double.parseDouble(textField_CustomAmount.getText()),
						LoanState.INVESTMENT,
						_memMan.get_idFromName("Household"),
						Date.getCurrentDate().toString(),
						dueDate.toString(),
						Double.parseDouble(textField_CustomIR.getText()), DEFAULT_PARTYINVOLVED, "");
				
				shell.readCmd(cmd);
				_panel_loanMgr.updateTable();
				updateAll();
				
				JOptionPane.showMessageDialog(InvestmentPlanPanel.this, "Investment has been added and details can be tracked on Loan panel.");
				
				// Clear all fields
				clearAll();
			}
		});
		btnAddSavings.setVisible(false);
		
		lblPromptAddInvestment = new JLabel("Add Investment?");
		lblPromptAddInvestment.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptAddInvestment.setBounds(314, 486, 105, 15);
		lblPromptAddInvestment.setVisible(false);
		lblPromptAddInvestment.setFont(new Font("Lucida Grande", Font.BOLD, 12));
		this.add(lblPromptAddInvestment);
		
		textField_YearsToInvest = new JTextField();
		textField_YearsToInvest.setBounds(429, 512, 289, 28);
		textField_YearsToInvest.setHorizontalAlignment(SwingConstants.TRAILING);
		textField_YearsToInvest.setVisible(false);
		
		lblPromptYearsToInvest = new JLabel("Enter years to invest:");
		lblPromptYearsToInvest.setHorizontalAlignment(SwingConstants.TRAILING);
		lblPromptYearsToInvest.setBounds(287, 518, 132, 16);
		lblPromptYearsToInvest.setVisible(false);
		
		comboBox_InvestmentDecision = new JComboBox();
		comboBox_InvestmentDecision.setBounds(429, 480, 289, 27);
		comboBox_InvestmentDecision.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent comboBoxDecisionMade) {
				if (comboBox_InvestmentDecision.getSelectedItem().toString() == "Yes") {
					// Set all fields dedicated to the time checks comboBox
					// And the three time periods textfields to not visible
					lblNumberTimeChecks.setVisible(false);
					comboBox_NumberTimeChecks.setVisible(false);
					lblPromptFirstTimePeriod.setVisible(false);
					textField_FirstTimePeriod.setVisible(false);
					lblPromptSecondTimePeriod.setVisible(false);
					textField_SecondTimePeriod.setVisible(false);
					lblPromptThirdTimePeriod.setVisible(false);
					textField_ThirdTimePeriod.setVisible(false);
					textField_FirstTimePeriod.setText("");
					textField_SecondTimePeriod.setText("");
					textField_ThirdTimePeriod.setText("");
					btnCalculateSavings.setVisible(false);
					
					// Set years to invest fields and "Add" button to visible
					lblPromptYearsToInvest.setVisible(true);
					textField_YearsToInvest.setVisible(true);
					btnAddSavings.setVisible(true);
				}
				else if (comboBox_InvestmentDecision.getSelectedItem().toString() == "No") {
					// Clear all fields
					clearAll();
				}
			}
		});
		comboBox_InvestmentDecision.setVisible(false);
		comboBox_InvestmentDecision.setModel(new DefaultComboBoxModel(new String[] {"(Choose option)", "Yes", "No"}));
		this.add(comboBox_InvestmentDecision);
		this.add(lblPromptYearsToInvest);
		this.add(textField_YearsToInvest);
		textField_YearsToInvest.setColumns(10);
		this.add(btnAddSavings);
		
		JButton btnClearAll = new JButton("Clear All");
		btnClearAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent allCleared) {
			clearAll();
			}
		});
		btnClearAll.setBounds(26, 140, 91, 29);
		add(btnClearAll);
		
		separator = new JSeparator();
		separator.setBounds(26, 90, 798, 10);
		add(separator);
		updateAll();
	}
	
	
	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	private void clearAll() {
		comboBox_AmountToInvest.setSelectedItem("(Choose amount)");
		comboBox_IR.setSelectedItem("(Choose interest rate)");
		comboBox_NumberTimeChecks.setSelectedItem("(Choose number)");
		comboBox_InvestmentDecision.setSelectedItem("(Choose option)");
		
		textField_CustomAmount.setText("");
		textField_CustomIR.setText("");
		textField_CustomAmount.setVisible(false);
		lblPromptIR.setVisible(false);
		comboBox_IR.setVisible(false);
		textField_CustomIR.setVisible(false);
		lblPromptCustomAmount.setVisible(false);
		lblPromptCustomIR.setVisible(false);
		lblPromptCustomName.setVisible(false);
		textField_CustomName.setVisible(false); 
		
		
		lblNumberTimeChecks.setVisible(false);
		comboBox_NumberTimeChecks.setVisible(false);
		lblPromptFirstTimePeriod.setVisible(false);
		textField_FirstTimePeriod.setVisible(false);
		lblPromptSecondTimePeriod.setVisible(false);
		textField_SecondTimePeriod.setVisible(false);
		lblPromptThirdTimePeriod.setVisible(false);
		textField_ThirdTimePeriod.setVisible(false);
		textField_FirstTimePeriod.setText("");
		textField_SecondTimePeriod.setText("");
		textField_ThirdTimePeriod.setText("");
		btnCalculateSavings.setVisible(false);
		
		lblPromptAddInvestment.setVisible(false);
		comboBox_InvestmentDecision.setVisible(false);
		lblPromptYearsToInvest.setVisible(false);
		textField_YearsToInvest.setVisible(false);
		textField_YearsToInvest.setText("");
		btnAddSavings.setVisible(false);
		
	}
	
	/**
	 * @param
	 * @Precondition 		
	 * @Postcondition 
	 */
	public boolean validatePaymentAnalysis() {
		// If currentIncomeSurplus is negative, display error message
		if (_currentIncomeSurplus < 0) {
			JOptionPane.showMessageDialog(InvestmentPlanPanel.this, "You have a negative income surplus and are not in position to buy any additional items.");
			return false;
		}
		
		
		if(!ValidateInput.validInvestmentAmount(textField_targetPurchase.getText(), "", _currentIncomeSurplus, "Payment Analysis")) {
			return false;
		}
		
		return true;
	}
	
	public boolean validateSavingsAnalysis() {
		// If currentIncomeSurplus is negative, display error message
		if (_currentIncomeSurplus < 0) {
			//textField_targetPurchase.setText("");
			JOptionPane.showMessageDialog(InvestmentPlanPanel.this, "You have a negative income surplus and are not in position to invest any moeny.");
			return false;
		}
		
		if(!ValidateInput.validInvestmentAmount(textField_CustomAmount.getText(), "amount ", _currentIncomeSurplus, "Savings Analysis")) {
			return false;
		}
		if(!ValidateInput.validName(textField_CustomName.getText())) {
			return false;
		}
		if(!ValidateInput.validInterestRate(textField_CustomIR.getText(), "Savings Analysis")) {
			return false;
		}
		int numberOfChecks = (int) Integer.parseInt(comboBox_NumberTimeChecks.getSelectedItem().toString());
		
		if(!ValidateInput.validYears(textField_FirstTimePeriod.getText(), "first")) {
			return false;
		}
		
		if (numberOfChecks  >= 2) {
			if(!ValidateInput.validYears(textField_SecondTimePeriod.getText(), "second")) {
				return false;
			}
		}
		if (numberOfChecks == 3) {
			if(!ValidateInput.validYears(textField_ThirdTimePeriod.getText(), "third")) {
				return false;
			}
		}
		
		return true;
	}
	
	public boolean validateInvestmentAdd() {
		// If currentIncomeSurplus is negative, display error message
		if (_currentIncomeSurplus < 0) {
			JOptionPane.showMessageDialog(InvestmentPlanPanel.this, "You have a negative income surplus and are not in position to invest any money.");
			return false;
		}
		
		if(!ValidateInput.validInvestmentAmount(textField_CustomAmount.getText(), "amount ", _currentIncomeSurplus, "Savings Analysis")) {
			return false;
		}
		if(!ValidateInput.validName(textField_CustomName.getText())) {
			return false;
		}
		if(!ValidateInput.validInterestRate(textField_CustomIR.getText(), "Savings Analysis")) {
			return false;
		}
		if(!ValidateInput.validYears(textField_YearsToInvest.getText(), "investment")) {
			return false;
		}
		return true;
	}
	
	/**
	 * @param
	 * @Precondition investment has been added, other update function has been called, or "Clear All" button has been selected		
	 * @Postcondition updated values for currentIncomeSurplus and currentSaved, updated display of these two variables on the screen
	 */
	public void updateAll()
	{
		_currentIncomeSurplus = _incomeMan.getTotalAmountOfAll() - _expenseMan.getTotalAmountOfAll() - _loanMan.get_valueInvested() - _loanMan.get_amountFromLoansLent();	
		if (_currentIncomeSurplus >= 0) {
			lblCurrentIncomeSurplus.setText("$" + DataFormat.returnTwoDecimals(_currentIncomeSurplus));
		}
		else {
			lblCurrentIncomeSurplus.setText("-$" + DataFormat.returnTwoDecimals(Math.abs(_currentIncomeSurplus)));
		}

		_currentSaved = _loanMan.get_valueInvested();
		lblcurrSaved.setText("$" + DataFormat.returnTwoDecimals(_currentSaved));
		
		clearAll();
	}

	/**
	 * @param
	 * @Precondition 		
	 * @Postcondition updated values for currentIncomeSurplus and currentSaved, updated display of these two variables on the screen
	 */
	public void updateIncome() {
		updateAll();
	}
	
	/**
	 * @param
	 * @Precondition 		
	 * @Postcondition updated values for currentIncomeSurplus and currentSaved, updated display of these two variables on the screen
	 */
	public void updateExpense() {
		updateAll();
	}
	/**
	 * @param
	 * @Precondition 		
	 * @Postcondition updated values for currentIncomeSurplus and currentSaved, updated display of these two variables on the screen
	 */
	public void updateLoan() {
		updateAll();
	}

	public void updateCategory() {
	}

	@Override
	public void updateMember() {
	}

	@Override
	public void updatePayMethod() {
	}

	@Override
	public void updateTable() {
		// TODO Auto-generated method stub
		
	}
}
